package pcjavanet.mobilesell.service;

import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;  
import java.util.Iterator; 
import java.util.Map;      
import pcjavanet.mobilesell.model.Goods;
import pcjavanet.mobilesell.model.GoodsExample;
import pcjavanet.mobilesell.model.Color;
import pcjavanet.mobilesell.model.ColorExample;
import pcjavanet.mobilesell.model.Configuration;
import pcjavanet.mobilesell.model.ConfigurationExample;
import pcjavanet.mobilesell.model.Sale; 
import pcjavanet.mobilesell.model.PageBean;
import pcjavanet.mobilesell.model.SaleExample;
import pcjavanet.mobilesell.model.QryCondition;
import pcjavanet.mobilesell.model.QryTerm;
import pcjavanet.mobilesell.persistence.GoodsMapper;
import pcjavanet.mobilesell.persistence.ColorMapper;
import pcjavanet.mobilesell.persistence.ConfigurationMapper;
import pcjavanet.mobilesell.persistence.SaleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
public class SaleService {


  @Autowired
  private  SaleMapper saleMapper;
  @Autowired
  private  GoodsMapper goodsMapper;
  @Autowired
  private  ColorMapper colorMapper;
  @Autowired
  private  ConfigurationMapper configurationMapper;


  public int  insert( Sale sale ) throws Exception{
	return saleMapper.insert (sale);
  }


  public Sale  selectByPrimaryKey( Sale  bean ) {
 	int dbid = bean.getDbid();
	Sale sale = saleMapper.selectByPrimaryKey( dbid );
	Goods goods = goodsMapper.selectByPrimaryKey( sale.getRefGoodsDbid() );
	sale.setGoods(goods) ;
	Color color = colorMapper.selectByPrimaryKey( sale.getRefColorDbid() );
	sale.setColor(color) ;
	Configuration configuration = configurationMapper.selectByPrimaryKey( sale.getRefConfigurationDbid() );
	sale.setConfiguration(configuration) ;
	return sale ;
  }


  public int  updateByPrimaryKeySelective( Sale sale ) throws Exception{
	return saleMapper.updateByPrimaryKeySelective( sale );
  }


  public int  deleteByPrimaryKey( Sale  bean ) throws Exception{
 	int dbid = bean.getDbid();
	return saleMapper.deleteByPrimaryKey( dbid );
  }


  public List<Sale>  selectByExample( Sale sale ) throws Exception{
    SaleExample example = new SaleExample();
	return saleMapper.selectByExample( example );
  }

  public List<Sale>  getOnePageBeans( QryCondition condition , PageBean pageBean ) throws Exception{
    SaleExample example = new SaleExample();
    ServiceUtil.dealCriteria( condition,  example.createCriteria() );
	List<Sale> ls=saleMapper.getOnePageBeans( example , pageBean );
//begin
	if ( ls.size() > 0 ) {
  		Map<Integer,Integer> goodsIdmap = new HashMap<Integer,Integer>();
  		for(Sale  m : ls){
  			m.setGoods(new Goods ()) ; 
  			if ( m.getRefGoodsDbid() != null ) 
  				goodsIdmap.put(m.getRefGoodsDbid(), m.getRefGoodsDbid());
  		};
  		List<Integer> refGoodsDbids = new ArrayList<Integer>();
  		Iterator<Integer> refGoodsDbidit = goodsIdmap.keySet().iterator(); 
  		while ( refGoodsDbidit.hasNext() ){
  			refGoodsDbids.add( refGoodsDbidit.next() );
  		}
  		GoodsExample goodsExample = new GoodsExample();
  		if ( refGoodsDbids.size() > 0 ) 
  			goodsExample.createCriteria().andDbidIn( refGoodsDbids );
  		List<Goods> goodss = goodsMapper.selectByExample( goodsExample);
  		for(Sale m : ls ){
  			for(Goods c : goodss ){
  				if ( m.getRefGoodsDbid() !=null && c.getDbid() !=null && m.getRefGoodsDbid().intValue() == c.getDbid().intValue() ) {
  					m.setGoods( c );
  				}
  			}
  		}
  		Map<Integer,Integer> colorIdmap = new HashMap<Integer,Integer>();
  		for(Sale  m : ls){
  			m.setColor(new Color ()) ; 
  			if ( m.getRefColorDbid() != null ) 
  				colorIdmap.put(m.getRefColorDbid(), m.getRefColorDbid());
  		};
  		List<Integer> refColorDbids = new ArrayList<Integer>();
  		Iterator<Integer> refColorDbidit = colorIdmap.keySet().iterator(); 
  		while ( refColorDbidit.hasNext() ){
  			refColorDbids.add( refColorDbidit.next() );
  		}
  		ColorExample colorExample = new ColorExample();
  		if ( refColorDbids.size() > 0 ) 
  			colorExample.createCriteria().andDbidIn( refColorDbids );
  		List<Color> colors = colorMapper.selectByExample( colorExample);
  		for(Sale m : ls ){
  			for(Color c : colors ){
  				if ( m.getRefColorDbid() !=null && c.getDbid() !=null && m.getRefColorDbid().intValue() == c.getDbid().intValue() ) {
  					m.setColor( c );
  				}
  			}
  		}
  		Map<Integer,Integer> configurationIdmap = new HashMap<Integer,Integer>();
  		for(Sale  m : ls){
  			m.setConfiguration(new Configuration ()) ; 
  			if ( m.getRefConfigurationDbid() != null ) 
  				configurationIdmap.put(m.getRefConfigurationDbid(), m.getRefConfigurationDbid());
  		};
  		List<Integer> refConfigurationDbids = new ArrayList<Integer>();
  		Iterator<Integer> refConfigurationDbidit = configurationIdmap.keySet().iterator(); 
  		while ( refConfigurationDbidit.hasNext() ){
  			refConfigurationDbids.add( refConfigurationDbidit.next() );
  		}
  		ConfigurationExample configurationExample = new ConfigurationExample();
  		if ( refConfigurationDbids.size() > 0 ) 
  			configurationExample.createCriteria().andDbidIn( refConfigurationDbids );
  		List<Configuration> configurations = configurationMapper.selectByExample( configurationExample);
  		for(Sale m : ls ){
  			for(Configuration c : configurations ){
  				if ( m.getRefConfigurationDbid() !=null && c.getDbid() !=null && m.getRefConfigurationDbid().intValue() == c.getDbid().intValue() ) {
  					m.setConfiguration( c );
  				}
  			}
  		}
	}
//end
	return ls;
  }

  public int getTotalRecords( QryCondition condition , PageBean pageBean ) throws Exception{
    SaleExample example = new SaleExample();
    ServiceUtil.dealCriteria( condition,  example.createCriteria() );
	return saleMapper.getTotalRecords( example , pageBean );
  }

  public int countByExample( Sale sale , PageBean pageBean ){
    SaleExample example = new SaleExample();
	return saleMapper.countByExample( example );
  }
  public List<Sale> selectByExamplePage( Sale sale , PageBean pageBean ){
  SaleExample example = new SaleExample();
	return saleMapper.selectByExamplePage( example , pageBean );
  }

}
